level: beginner
Eddie's Lessons
Beginners Programming
For more information on the RUN command, see issue #114.
The RUN Command
Liberty BASIC allows us to run other programs with the RUN command. There is one required argument, which is a literal string or a string variable that contains the filename of the executable file to run. The second argument is optional and contains the mode to run the file.
RUN filename$ [, mode ]
Here is an example:
run "notepad"
That's pretty simple, isn't it? Windows knows where all of the standard programs are stored, so we don't even need to use the full path. We also don't need to include the extension "exe" but we can include it if we want to.
run "calc.exe" run "calc" run "notepad" run "mspaint"
The Liberty BASIC program continues to execute when it runs another program. Running another program with the RUN command does not halt a Liberty BASIC program.
We can run any program, but if Windows does not know where to find it, we must include the drive, folder, and filename information. Here's an example:
run "c:\My Files\Myprog.exe"
MODE
If the RUN command invokes a Windows program, it can also contain a mode parameter to specify how the program will be displayed. To run Notepad maximized:
run "notepad", SHOWMAXIMIZED
Here are the possible values for the optional MODE:
HIDE (activates the application, but hides it from the user)
SHOWNORMAL (this is the default - activates and shows the application)
SHOWMINIMIZED (activates the application, but minimizes it to the taskbar)
SHOWMAXIMIZED (activates the application in a full screen window)
SHOWNOACTIVE (shows a window in its most recent size and position without activating it)
SHOW (activates an application and shows it in its current size and position)
MINIMIZE (minimizes a window and activates the next window in the Z order)
SHOWMINNOACTIVE (minimizes an application, leaving the current active window active)
SHOWNA (displays the window in its current state. the active window remains active)
RESTORE (activates the application and displays the window)
What is the Command Line?
The Command Line is the information sent to a program when it is invoked. It might contain a filename or other parameters and instructions. Here is an example. To run an executable program with a file loaded, include the filename after the name of the executable. This example, when run from the Liberty BASIC 4 root directory, runs Notepad maximized with the "newfor4.txt" file loaded:
run "notepad newfor4.txt", SHOWMAXIMIZED
Quotation Marks, Blank Spaces and Filenames
Long filenames may contain spaces. Even if the filename itself does not contain spaces, it is likely that some part of the drive and folder information contains spaces. This is typical:
C:\Program Files\My Cool Program\myprog.exe
The command line is often parsed (evaluated) by breaking it into pieces at the blank spaces. To keep a filename with spaces together so that it is interpreted as a single entity, place it inside of double quotation marks, which are chr$(34). Append the filename string to the name of the executable. Be sure to leave a blank space after the executable's name.
run "notepad " + chr$(34) + DefaultDir$ + "\myfile.txt" + chr$(34)
Command Line Switches
Some programs use commandline switches. In issue #103, we discussed the use of the /p switch with MS Paint to cause a hard copy of a designated bitmap to be printed by mspaint.exe. Here is a small, working demo of that technique.
filedialog "Open","*.bmp",BmpFile$ run "mspaint.exe " + BmpFile$ + " /p", HIDE
The available switches depend on the application being run. Liberty BASIC has some command line switches.
From the Liberty BASIC Helpfile:
You may use a third-party editor to create and modify Liberty BASIC code. You can even write your own code editor in Liberty BASIC! In order to run the code with Liberty BASIC from another program, use the following startup options to LIBERTY.EXE:
' -R Run a BAS file on startup
' -D Debug a BAS file on startup
'--------the following three are in the registered version only--------
' -T Make a TKN file from a BAS file on startup
' -A Automatically Exit LB on completion of BAS file
' -M Minimize the Liberty BASIC editor on startup
As it appears when used in a code editor written in Liberty BASIC:
RUN "LIBERTY -R -M PROG.BAS" 'run PROG.BAS with editor minimized RUN "LIBERTY -T -A PROG.BAS" 'create a TKN file from PROG.BAS then exit RUN "LIBERTY -D PROG.BAS" 'run the debugger on PROG.BAS
Running Liberty BASIC!
[run]
'go to routine to get liberty.exe path
'into variable LibertyExe$$
gosub [GetLibertyPath]
'if we can't find the path to Liberty BASIC
'we must abort the RUN operation
if LibertyExe$="" then wait
'get contents of texteditor into string variable
#1.te "!contents? text$"
'if there is no code, then abort the RUN operation
if text$="" then
notice "No code to run."
wait
end if
'open file and write text to it
open tempfile$ for output as #f
print #f, text$
close #f
'use RUN command to run Liberty BASIC
'with this file loaded
'put filename in quotation marks, so
'that spaces do not break up the filename
RUN LibertyExe$ + " " + chr$(34) + tempfile$ + chr$(34)
wait
Eddie's Lessons
Beginners Programming